package data.nanjing.expo.services;

import data.nanjing.expo.models.dao.ActivityInfoDao;
import data.nanjing.expo.models.dao.ActivityTagInfoDao;
import data.nanjing.expo.models.domain.ActivityInfo;
import data.nanjing.expo.models.domain.ActivityInfoDetail;
import data.nanjing.expo.models.domain.SysConfig;
import data.nanjing.expo.models.domain.dto.ActivityInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by zjp on 2018/6/24.
 * QQ：34948062
 * github: https://www.github.com/zjp1029
 * web： http://www.zjp1029.cn
 */
@Service
public class ActivityInfoService {

    @Autowired
    private ActivityInfoDao activityInfoDao;

    @Autowired
    private ActivityTagInfoDao activityTagInfoDao;

    /**
     * 查询最近的五个活动
     * @return
     */
    public List<ActivityInfo> selectLatestActivityInfoList() {
        return activityInfoDao.selectLatestActivityInfoList();
    }

    /**
     * 查询活动列表
     * @param industry
     * @param isFree
     * @param datetime
     * @return
     */
    public List<ActivityInfo> selectActivityList(String industry, String isFree, Date datetime, Integer pageNumber, Integer pageSize) {
        if ("不限".equals(industry)) {
            industry = null;
        }
        if ("any".equals(isFree)) {
            isFree = null;
        }
        int pageStart = (pageNumber - 1) * pageSize;
        return activityInfoDao.selectActivityInfoList(industry, isFree, datetime, pageStart, pageSize + 1);
    }

    //根据id查询活动详情
    public ActivityInfoDto selectActivityDetailInfoById(Integer id){
        ActivityInfoDto activityInfoDto = activityInfoDao.selectActivityDetailInfoById(id);
        if (activityInfoDto != null) {
            List<SysConfig> list = activityTagInfoDao.selectTagInfoByActivityId(id);
            activityInfoDto.setTags(list);
        }
        return activityInfoDto;
    }

    //推荐;现在没实现
    public List<ActivityInfo> selectRecommendActivity() {
        return activityInfoDao.selectRecommendActivity();
    }

    //首页
    public List<ActivityInfo> selectHotActivityList() {
        return activityInfoDao.selectHotActivityList();
    }

    //根据用户id查询正在进行或已结束的活动列表
    public List<ActivityInfo> selectActivityListByUserIdAndIsWork(Integer userId, Integer pageNumber, Integer pageSize, boolean isWork) {
        int pageStart = (pageNumber - 1) * pageSize;
        if (isWork) {
            return activityInfoDao.selectWorkActivityListByUserId(userId, pageStart, pageSize);
        }else {
            return activityInfoDao.selectEndActivityListByUserId(userId, pageStart, pageSize);
        }
    }


    //添加活动内容
    @Transactional
    public Integer addActivityInfo(ActivityInfo activityInfo, ActivityInfoDetail activityInfoDetail) {
        activityInfoDao.addActivityInfo(activityInfo); //添加主表内容
        activityInfoDetail.setActivityId(activityInfo.getId());
        activityInfoDao.addActivityDetailInfo(activityInfoDetail);   //详细表内容
        return activityInfo.getId();
    }

    //更新活动
    @Transactional
    public void updateActivityInfo(ActivityInfo activityInfo, ActivityInfoDetail activityInfoDetail) {
        activityInfoDao.updateActivityInfo(activityInfo);
        activityInfoDao.updateActivityDetail(activityInfoDetail);
    }


    //删除活动(假删除)
    @Transactional
    public void deleteActivityInfoById(Integer activityId) {
        activityInfoDao.deleteActivityById(activityId);
    }

    //更新活动状态
    @Transactional
    public void updateActivityStatus(Integer activityId, String status) {
        activityInfoDao.updateActivityStatus(activityId, status);
    }

    public ActivityInfo selectActivityInfoById(Integer activityId) {
        return activityInfoDao.selectActivityInfoById(activityId);
    }

    //总数获取
    public Integer selectWorkActivityCountByUserId(Integer userId) {
        return activityInfoDao.selectWorkActivityCountByUserId(userId);
    }

    //总数获取
    public Integer selectEndActivityCountByUserId(Integer userId) {
        return activityInfoDao.selectEndActivityCountByUserId(userId);
    }

}
